package com.flame.workflow.ru.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.flame.core.utils.DateUtils;
import com.flame.workflow.constants.RuntimeConstants;
import com.flame.workflow.constants.TaskConstants;
import com.flame.workflow.ru.entity.Task;
import com.flame.workflow.ru.entity.TaskProperty;
import com.flame.workflow.ru.mapper.TaskMapper;
import com.flame.workflow.ru.mapper.TaskPropertyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * flame_workflow_ru_task 流程实例任务表
 *
 * @author 吴欣童
 * @since 2023-06-05 14:28:28
 */
@Service
public class TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private TaskPropertyMapper taskPropertyMapper;

    public void updateTask(Task task) {
        taskMapper.updateById(task);
    }

    public Task getTask(Long taskId) {
        Task task = taskMapper.findById(taskId);
        task.setTaskProperties(getTaskProperties(taskId));
        return task;
    }

    private Map<String, String> getTaskProperties(Long taskId) {
        List<TaskProperty> taskPropertyList = taskPropertyMapper.listByTaskId(taskId);

        Map<String, String> map = new HashMap<>();
        for (TaskProperty taskProperty : taskPropertyList) {
            map.put(taskProperty.getPropertyKey(), taskProperty.getPropertyValue());
        }
        return map;
    }

    public void deleteTask(Long taskId) {
        Task task = new Task();
        task.setTaskId(taskId);
        task.setIsDeleted(TaskConstants.DELETED);
        taskMapper.updateById(task);
    }

    public List<Task> listByBranchTaskId(Long taskId) {
        List<Task> taskList = taskMapper.listByBranchTaskId(taskId);
        for (Task task : taskList) {
            task.setTaskProperties(getTaskProperties(task.getTaskId()));
        }
        return taskList;
    }

    public void addTask(Task task) {
        task.setIsDeleted(TaskConstants.NOT_DELETED);
        task.setCreateTime(DateUtils.currentDateTime());
        taskMapper.insert(task);
    }

    public List<Task> listByInstanceId(Long instanceId) {
        List<Task> taskList = taskMapper.listByInstanceId(instanceId);
        for (Task task : taskList) {
            task.setTaskProperties(getTaskProperties(task.getTaskId()));
        }
        return taskList;
    }

    public Task getTaskByNodeId(Long instanceId, int nodeId) {
        Task task = taskMapper.findByNodeId(instanceId, nodeId);
        task.setTaskProperties(getTaskProperties(task.getTaskId()));
        return task;
    }

    public List<Task> listByParentTaskId(Long taskId) {
        List<Task> taskList = taskMapper.listByParentTaskId(taskId);
        for (Task task : taskList) {
            task.setTaskProperties(getTaskProperties(task.getTaskId()));
        }
        return taskList;
    }

    public List<Task> listIncompleteTaskByInstanceId(Long instanceId) {
        List<Task> taskList = taskMapper.listByInstanceId(instanceId);
        return taskList.stream().filter(task -> {
            if (RuntimeConstants.RUNTIME_STATUS_CREATE.equals(task.getTaskStatus())) {
                return true;
            } else if (RuntimeConstants.RUNTIME_STATUS_RUN.equals(task.getTaskStatus())) {
                return true;
            } else if (RuntimeConstants.RUNTIME_STATUS_WAIT.equals(task.getTaskStatus())) {
                return true;
            } else {
                return false;
            }
        }).collect(Collectors.toList());
    }
}
